package com.testDemo.design.chain;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class  RequestWrap implements InvocationHandler {

    private Object Request;

    private Filter Filter;

     public RequestWrap(Object Request,Filter Filter) {
        this.Request=Request;
        this.Filter=Filter;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        return Filter.filter((Request)Request);
    }

    public static Object wrap(Object target, Filter Filter) {

        Class<?> type = target.getClass();
        Class<?>[] interfaces = type.getInterfaces();
        if (interfaces.length > 0) {
            return Proxy.newProxyInstance(
                    type.getClassLoader(),
                    interfaces,
                    new RequestWrap(target, Filter));
        }
        return target;
    }


}
